<template>
  <div>
    <el-form ref="addleaveform" :rules="addRules" :model="leaveData" label-width="120px">
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="leaveData.name" placeholder="请输入等级名称" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="等级经验" prop="min_exp"> 
        <el-input v-model.number="leaveData.min_exp" placeholder="请输入等级经验" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="奖励比例(%)" prop="reward_ratio">
        <el-input v-model.number="leaveData.reward_ratio" placeholder="请输入奖励比例" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="leaveData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import store from '@/store'
import { postTuanLeave, putTuanLeave } from '@/api/customers'

export default {
  name: 'addLeave',
  props: {
    isAdd: {// 是否是新增
      type: Boolean,
      required: true,
      default: true
    },
    leaveData: {
      type: Object,
      default: () => {
        return {
          key: store.state.app.activeApp.saa_key,
          type: 2,
          min_exp: "",
          name: "",
          reward_ratio: "",
          status: "1"
        }
      }
    }
  },
  data() {
    return {
      addRules: {
        name: [
          { required: true, message: '请输入等级名称', trigger: 'blur' }
        ],
        min_exp: [
          { required: true, message: '请输入等级经验', trigger: 'blur' },
          { type: 'number', message: '等级经验必须为数字', trigger: 'blur' }
        ],
        reward_ratio: [
          { required: true, message: '请输入奖励比例', trigger: 'blur' },
          { type: 'number', message: '奖励比例必须为数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addleaveform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postTuanLeave(this.leaveData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            console.log(this.leaveData)
            putTuanLeave(this.leaveData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    }
  },
}
</script>